/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.icatportal.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.StatusButtonBar;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.Timer;

/**
 *
 * @author ll56
 */
public class SsoLogin extends Dialog {

    protected StatusButtonBar button2Bar;
    protected TextField<String> userName;
    protected TextField<String> password;
    protected Button reset;
    protected Button login;

    public SsoLogin() {

        FormLayout layout = new FormLayout();
        layout.setLabelWidth(90);
        layout.setDefaultWidth(155);
        setLayout(layout);

        setButtons("");
        setIconStyle("user");
        setHeading("ICAT Portal Prototype Login");
        setModal(true);
        setBodyBorder(true);
        //setInsetBorder(true);
        setBodyStyle("padding: 8px;background: none");
        setWidth(300);
        setResizable(false);

        KeyListener keyListener = new KeyListener() {

            @Override
            public void componentKeyUp(ComponentEvent event) {
                validate();
            }
        };

        userName = new TextField<String>();
        userName.setMinLength(4);
        userName.setFieldLabel("Username");
        userName.addKeyListener(keyListener);
        add(userName);

        password = new TextField<String>();
        password.setMinLength(4);
        password.setPassword(true);
        password.setFieldLabel("Password");
        password.addKeyListener(keyListener);
        add(password);

        setFocusWidget(userName);

        button2Bar = new StatusButtonBar();
        setButtonBar(button2Bar);

    }

    @Override
    protected void createButtons() {
        reset = new Button("Reset");
        reset.addSelectionListener(new SelectionListener<ButtonEvent>() {

            public void componentSelected(ButtonEvent ce) {
                userName.reset();
                password.reset();
                validate();
                userName.focus();
            }
        });

        login = new Button("Login");
        login.disable();
        login.addSelectionListener(new SelectionListener<ButtonEvent>() {

            public void componentSelected(ButtonEvent ce) {
                onSubmit();
            }
        });

        button2Bar.add(reset);
        button2Bar.add(login);
    }

    protected void onSubmit() {
        button2Bar.getStatusBar().showBusy("Please wait...");
        button2Bar.disable();
        Timer t = new Timer() {

            @Override
            public void run() {
                // TODO  Validate with SSO and collect the session ID


                SsoLogin.this.hide();
            }
        };
        t.schedule(2000);
    }

    protected boolean hasValue(TextField<String> field) {
        return field.getValue() != null && field.getValue().length() > 0;
    }

    protected void validate() {
        login.setEnabled(hasValue(userName) && hasValue(password) && password.getValue().length() > 3);
    }
}
